class Compiler {
  constructor(vm) {
    this.el = vm.$el
    this.vm = vm
    this.compile(this.el)
  }

  // 编译模板，处理文本节点和元素节点
  compile(el) {
    let childNodes = el.childNodes
    Array.from(childNodes).forEach(node => {
      // console.dir(node);
      if (this.isTextNode(node)) {
        // 处理文本节点
        this.compileText(node)
      } else if (this.isElementNode(node)) {
        // 处理元素节点
        this.compileElement(node)
      }

      // 判断node节点，是否有子节点，如果有子节点，要递归调用compile
      if (node.childNodes && node.childNodes.length) {
        this.compile(node)
      }
    })
  }

  // 编译元素节点，处理指令
  compileElement(node) {
    console.dir('编译元素节点，处理指令：', node);
    console.dir(node);
    console.log(node);
    Array.from(node.attributes).forEach(attr => {
      let attrName = attr.name
      // console.log(attr.name, '111', attrName);
      if (this.isDirective(attrName)) {
        // attrName = attrName.substr(2)
        // console.log('value:', attr.value);
        const key = attr.value
        // console.log('key:', key);
        this.update(node, key, attrName)
      }
    })
  }

  // 根据指令拼接函数 调用不同的指令处理函数
  update(node, key, attrName) {
    console.log(attrName, 'attrName');
    if (attrName.startsWith('v-on:')) {
      attrName = attrName.replace('v-on:', '')
    } else if (attrName.startsWith('@')) {
      attrName = attrName.replace('@', '')
    } else {
      attrName = attrName.substr(2)
    }
    console.log('new attrName', attrName);
    let updateFn = this[attrName + 'Updater']
    updateFn && updateFn.call(this, node, this.vm[key], key)
  }

  clickUpdater(node, value, key) {
    node.onclick = value
  }
  mouseoverUpdater(node, value, key) {
    node.onmouseover = value
  }

  textUpdater(node, value, key) {
    node.textContent = value

    new Watcher(this.vm, key, (newValue) => {
      node.textContent = newValue
    })
  }
  // v-html 
  htmlUpdater(node, value, key) {
    node.innerHTML = value

    new Watcher(this.vm, key, (newValue) => {
      node.innerHTML = newValue
    })
  }
  // v-model
  modelUpdater(node, value, key) {
    node.value = value
    new Watcher(this.vm, key, (newValue) => {
      node.value = newValue
    })

    // 双向绑定
    node.addEventListener('input', () => {
      this.vm[key] = node.value
    })
  }

  // 编译文本节点，处理差值表达式
  compileText(node) {
    console.dir('编译文本节点，处理差值表达式', node);
    // 差值表达式 {{}}
    let reg = /\{\{(.+?)\}\}/
    let value = node.textContent
    console.log(222, value);
    // 如果文本节点是差值表达式，就替换内容
    if (reg.test(value)) {
      let key = RegExp.$1.trim()
      console.log('key:', key, value.replace(reg, this.vm[key]));
      node.textContent = value.replace(reg, this.vm[key])

      new Watcher(this.vm, key, (newValue) => {
        console.log(newValue, 'ddddd');
        node.textContent = value.replace(reg, this.vm[key])
      })
    }
  }

  // 判断元素属性是否是指令
  isDirective(attrName) {
    // console.log(attrName, '1111114');
    return attrName.startsWith('v-') || attrName.startsWith('@')
  }

  // 判断节点是否是文本节点
  isTextNode(node) {
    return node.nodeType === 3
  }

  // 判断节点是否元素节点
  isElementNode(node) {
    return node.nodeType === 1
  }
}